package com.ykdl.member.kid.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.base.BaseObjectListAdapter;
import com.ykdl.member.kid.beans.OnLineCouponBean;
import com.ykdl.member.kid.beans.TicketBean;
import com.ykdl.member.kid.chat.ChatActivity;
import com.ykdl.member.kid.chat.CommunicateAcivity;
import com.ykdl.member.kid.gears.ShowFragmentActivity;
import com.ykdl.member.kid.marketcard.CouponinfoWebView;
import com.ykdl.member.kid.util.DisposalListUtils.ItemBean;
import com.ykdl.member.views.CircularImage;
/********************************************************************************

** 作者： jiafeiyu
** 创始时间：2015-4-28
** 描述：
**    门店货架--之优惠券adapter
 * @param <E>

*********************************************************************************/
public class Coupon_New_FragmentAdapter extends BaseAdapter {
	private List<TicketBean>  mOffLineList;
	private List<OnLineCouponBean>  mOnLineList;
	private BitmapUtils bitmapUtil ;
	private LayoutInflater inflater;
	private onSelectListener mOnSelectListener;
	private onCheckListener mSelectListener;
	private List<Integer> mCheckedList;
	private Context mContext;
	private boolean isFirst = true;
	private List<ItemBean> mData;
	
	
	public void setmCouponList(List<OnLineCouponBean> mCouponList) {
		this.mOnLineList = mCouponList;
	}

	public void setData(List<TicketBean>  tickets){
		this.mOffLineList = tickets;
	}
	
	
	
	

	public void setmData(List<ItemBean> mData) {
		this.mData = mData;
	}

	/**
	 * 用于记录是否已经添加了
	 * @param mCheckedList
	 */
	public void setCheckedList(List<Integer> mCheckedList){
		this.mCheckedList = mCheckedList;
		if (mCheckedList==null) {
			mCheckedList = new ArrayList<Integer>();
		}
	}
	
	
	public Coupon_New_FragmentAdapter(Context context,onSelectListener mOnSelectListener,onCheckListener mSelectListener) {
		mContext=context;
		bitmapUtil = new BitmapUtils(context);
		bitmapUtil.configDefaultLoadingImage(R.drawable.defult_head_icon);
		bitmapUtil.configDefaultLoadFailedImage(R.drawable.defult_head_icon);
		
		inflater = LayoutInflater.from(context);
		// TODO Auto-generated constructor stub
		this.mOnSelectListener = mOnSelectListener;
		this.mSelectListener = mSelectListener;
	}

	@Override
	public int getCount() {
		
//		int num = 2;
//		if (mOnLineList!=null&&mOnLineList.size()>0) {
//			num +=mOnLineList.size();
//			isOnLineNoCoupon = false;
//		}else {
//			num+=1;
//			isOnLineNoCoupon = true;
//		}	
//		if (mOffLineList!=null&&mOffLineList.size()>0){
//			num +=mOffLineList.size();
//			isOffLineNoCoupon = false;
//		}else {
//			num+=1;
//			isOffLineNoCoupon = true;
//		}
		if (mData!=null&&mData.size()>0) {
			return mData.size();
		}
		return 0;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
/*		if (mOnLineList!=null) {
				onLineLevel = mOnLineList.size()+1;
		}
		if (mOffLineList!=null) {
			
			offLineLevel = mOffLineList.size()+1;
		}
		if (isOnLineNoCoupon) {
			onLineLevel = 2;
		}
		if (isOffLineNoCoupon) {
			offLineLevel = 4;
		}
		if (position==0) {
			convertView = initTitle(1);//标题
		}else if(position==1&&isOnLineNoCoupon){
			convertView = initNoCoupon();//没有在线优惠券
		}else if(!isOnLineNoCoupon&&position>0&&position<onLineLevel){//有在线优惠券
			convertView = initOnLineCoupon(position, convertView);
		}else if(position==onLineLevel){//标题
			
				
				convertView = initTitle(2);
			
		}else if(position==(onLineLevel+1)&&isOffLineNoCoupon){//没有到店优惠券
			convertView = initNoCoupon();//没有在线优惠券
		}else if(position>(onLineLevel+1)&&!isOffLineNoCoupon){//有到店支付优惠券
			convertView = initCoupon(position, convertView);
		}*/
		
		ItemBean itemBean = mData.get(position);
		
		switch (itemBean.item_choose) {
		case 0:
			convertView = initTitle(1,itemBean.isHaveCoupon);//标题
			break;

		case 1:
			convertView = initTitle(2,itemBean.isHaveCoupon);//标题
			break;
		case 2:
			convertView = initOnLineCoupon(itemBean.position, convertView,position);
			break;
		case 3:
			convertView = initCoupon(itemBean.position, convertView,position);
			break;
		case 4:
			convertView = initNoCoupon(4);//
			break;
		case 5:
			convertView = initNoCoupon(5);//
			break;
		default:
			break;
		}
		
		
		
		
		Log.i("CouponFragment", "position="+position);
		return convertView;
	}

//----------------------------------------到店支付可用优惠券---------------------------------------------------	
	/**
	 * 初始化 到店支付可用的优惠券
	 * @param position
	 * @param convertView
	 * @return
	 */
	public View initCoupon(int position, View convertView,final int mObjectPosition) {
		ViewHolder holder;
		Object tag = null;
		
		
		if (convertView!=null) {
			tag = convertView.getTag();
		}
		if(null!=tag&&tag instanceof ViewHolder ){
			holder = (ViewHolder) tag;
		}else {
			holder = new ViewHolder();
		
			convertView = inflater.inflate(R.layout.item_coupon_fragment_adapter, null);
			holder = new ViewHolder();
			holder.iv_coupon_fragment_back_logo  = (CircularImage) convertView.findViewById(R.id.iv_coupon_fragment_back_logo);
			holder.iv_coupon_fragment_vip = (ImageView) convertView.findViewById(R.id.iv_coupon_fragment_vip);
			holder.tv_coupon_fragment_xilie = (TextView) convertView.findViewById(R.id.tv_coupon_fragment_xilie);
			holder.ll_item_coupon_diyong = (LinearLayout) convertView.findViewById(R.id.ll_item_coupon_diyong);
			holder.tv_coupon_fragment_diyong_number = (TextView) convertView.findViewById(R.id.tv_coupon_fragment_diyong_number);
			holder.ll_item_coupon_manzeng = (LinearLayout) convertView.findViewById(R.id.ll_item_coupon_manzeng);
			holder.tv_coupon_fragment_manzeng_content = (TextView) convertView.findViewById(R.id.tv_coupon_fragment_manzeng_content);
			holder.ll_item_coupon_zhijiang = (LinearLayout) convertView.findViewById(R.id.ll_item_coupon_zhijiang);
			holder.tv_coupon_fragment_zhijiang_number =  (TextView) convertView.findViewById(R.id.tv_coupon_fragment_zhijiang_number);
			holder.ll_item_coupon_manjian = (LinearLayout) convertView.findViewById(R.id.ll_item_coupon_manjian);
			holder.tv_coupon_fragment_manjian_content = (TextView) convertView.findViewById(R.id.tv_coupon_fragment_manjian_content);
			holder.tv_coupon_fragment_manjian_number = (TextView) convertView.findViewById(R.id.tv_coupon_fragment_manjian_number);
		//	holder.tv_coupon_fragment__number = (TextView) convertView.findViewById(R.id.tv_coupon_fragment__number);
			holder.bt_coupon_fragment_get_coupon = (Button) convertView.findViewById(R.id.bt_coupon_fragment_get_coupon);
			holder.tv_coupon_fragment_is_get = (TextView) convertView.findViewById(R.id.tv_coupon_fragment_is_get);
			holder.ll_coupon_whole_back = (LinearLayout) convertView.findViewById(R.id.ll_coupon_whole_back);
			holder.rl_item_coupon_detail = (RelativeLayout) convertView.findViewById(R.id.rl_item_coupon_detail);
			convertView.setTag(holder);
		}
		initView(holder,position,mObjectPosition);
		return convertView;
	}
	//优惠劵类型:1  # 抵用  2  # 满减  3  # 买赠  4  # 直降
	 private void initView(ViewHolder holder, final int position,final int mObjectPosition) {
		 if (mOffLineList==null||mOffLineList.size()==0) 
			return ;
		 TicketBean bean = mOffLineList.get(position);
		 switch (bean.getCoupon_type()) {
		case 1:
			//--green
			
			holder.ll_coupon_whole_back.setBackgroundResource(R.drawable.coupon_green_back);
			holder.ll_item_coupon_diyong.setVisibility(View.VISIBLE);
			holder.ll_item_coupon_manzeng.setVisibility(View.GONE);
			holder.ll_item_coupon_zhijiang.setVisibility(View.GONE);
			holder.ll_item_coupon_manjian.setVisibility(View.GONE);
			holder.tv_coupon_fragment_diyong_number.setText(bean.getCoupon_num_1());
			
			
			break;
		case 2:
			//--red	
			
			holder.ll_coupon_whole_back.setBackgroundResource(R.drawable.coupon_red_back);
			holder.ll_item_coupon_diyong.setVisibility(View.GONE);
			holder.ll_item_coupon_manzeng.setVisibility(View.GONE);
			holder.ll_item_coupon_zhijiang.setVisibility(View.GONE);
			holder.ll_item_coupon_manjian.setVisibility(View.VISIBLE);
			
			holder.tv_coupon_fragment_manjian_content.setText("满"+bean.getCoupon_num_1()+"减");
			holder.tv_coupon_fragment_manjian_number.setText(bean.getCoupon_num_2());
			
			
			
			break;
		case 3:
			//--yellow
			
			holder.ll_coupon_whole_back.setBackgroundResource(R.drawable.coupon_yellow_back);
			holder.ll_item_coupon_diyong.setVisibility(View.GONE);
			holder.ll_item_coupon_manzeng.setVisibility(View.GONE);
			holder.ll_item_coupon_zhijiang.setVisibility(View.GONE);
			holder.ll_item_coupon_manjian.setVisibility(View.VISIBLE);
			
			holder.tv_coupon_fragment_manjian_content.setText("买"+bean.getCoupon_num_1()+"赠");
			holder.tv_coupon_fragment_manjian_number.setText(bean.getCoupon_num_2());
//			holder.tv_coupon_fragment_manzeng_content.setText("买"+bean.getCoupon_num_1()+"赠"+bean.getCoupon_num_2());
			
			
			break;
		case 4:
			//--blue
			
			holder.ll_coupon_whole_back.setBackgroundResource(R.drawable.coupon_blue_back);
			holder.ll_item_coupon_diyong.setVisibility(View.GONE);
			holder.ll_item_coupon_manzeng.setVisibility(View.GONE);
			holder.ll_item_coupon_zhijiang.setVisibility(View.VISIBLE);
			holder.ll_item_coupon_manjian.setVisibility(View.GONE);
			
			
			holder.tv_coupon_fragment_zhijiang_number.setText(bean.getCoupon_num_1());

			break;
		default:
			break;
		}
		
		 String url = bean.getLogo().getFile_url();
		 if (!TextUtils.isEmpty(url)) {
			 bitmapUtil.display(holder.iv_coupon_fragment_back_logo, url);
		 }
		 if (bean.isMembers_only()) {
			 holder.iv_coupon_fragment_vip.setVisibility(View.VISIBLE); 
			 
		 }else {
			 holder.iv_coupon_fragment_vip.setVisibility(View.GONE);
		 }
		
		
		 
		 
		 if (!bean.isReceive_flag()) {
			 holder.bt_coupon_fragment_get_coupon.setBackgroundResource(R.drawable.get_coupon_button);		 
			 if (bean.isMembers_only()) {			
				 if (bean.isMember_flag()) {
					 holder.tv_coupon_fragment_is_get.setText("立刻领取");
				 }else {
					 holder.tv_coupon_fragment_is_get.setText("加入领取");
				 }
				 
			 }else {
				 holder.tv_coupon_fragment_is_get.setText("立刻领取");
			}
			
		}else {
			holder.bt_coupon_fragment_get_coupon.setBackgroundResource(R.drawable.geted_coupon_button);
			holder.tv_coupon_fragment_is_get.setText("已领取");
		}
		 if (mCheckedList!=null&&mCheckedList.size()>0) {
				if (mCheckedList.contains(mObjectPosition)) {
					holder.bt_coupon_fragment_get_coupon.setBackgroundResource(R.drawable.geted_coupon_button);
					holder.tv_coupon_fragment_is_get.setText("已领取");
				}
			}
		 holder.tv_coupon_fragment_is_get.getPaint().setFakeBoldText(true); 
		 if (!TextUtils.isEmpty(bean.getDiscount_to())) {
			holder.tv_coupon_fragment_xilie.setText(bean.getDiscount_to());
		 }
		 holder.bt_coupon_fragment_get_coupon.setTag(R.id.account_wheel_time, holder);
		 holder.bt_coupon_fragment_get_coupon.setTag(R.id.accountinfor_wheel_time, bean);
		 holder.bt_coupon_fragment_get_coupon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ViewHolder holder =	(ViewHolder) v.getTag(R.id.account_wheel_time);
				TicketBean bean = (TicketBean) v.getTag(R.id.accountinfor_wheel_time);
				mOnSelectListener.onSelectListener(holder,bean,mObjectPosition);
			}
		});
		 holder.rl_item_coupon_detail.setTag(bean);
		 holder.rl_item_coupon_detail.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 进行查询优惠券详情的跳转
				TicketBean info = (TicketBean) v.getTag();
				Intent intent = new Intent(mContext, CouponinfoWebView.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra(KidAction.COUPON_ID, info.getCoupon_id());
				intent.putExtra(KidAction.URL_TYPE, 3);
				mContext.startActivity(intent);
			}
		});
		 
		 
	}

	 public class ViewHolder{
		 CircularImage iv_coupon_fragment_back_logo;
		ImageView iv_coupon_fragment_vip;
		TextView tv_coupon_fragment_xilie;
		//设置立即领取和已领取
		TextView tv_coupon_fragment_is_get;
		//抵用
		LinearLayout ll_item_coupon_diyong;
		TextView tv_coupon_fragment_diyong_number;
		//满赠
		LinearLayout ll_item_coupon_manzeng;
		TextView tv_coupon_fragment_manzeng_content;
		//直降
		LinearLayout ll_item_coupon_zhijiang;
		TextView tv_coupon_fragment_zhijiang_number;
		//满减
		LinearLayout ll_item_coupon_manjian;
		TextView tv_coupon_fragment_manjian_content;
		TextView tv_coupon_fragment_manjian_number;
		//整体
		LinearLayout ll_coupon_whole_back;
		
		TextView tv_coupon_fragment__number;	
		//点击按钮
		Button bt_coupon_fragment_get_coupon;
		//点击查询详情
		RelativeLayout rl_item_coupon_detail;
		
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	/**
	 * 点击选择进行提交或者是加入门店会员的接口
	 * @author jiafeiyu
	 *
	 */
	public interface onSelectListener{
		boolean onSelectListener(ViewHolder holder, TicketBean bean,int position);
	}
	/**
	 * 在线支付可用优惠券的领取事件
	 * @author jiafeiyu
	 *
	 */
	public interface onCheckListener{
		boolean onCheckListener(ViewHolderOnLine holder, OnLineCouponBean bean,int position);
	}
	private View getView(int RescoureID){
		return  inflater.inflate(RescoureID, null);
	}
	private String store_id;
//----------------------------------------标题---------------------------------------------------
	private View initTitle(int number,boolean isHaveCoupon){
		View view = getView(R.layout.item_coupon_line_title);
		TextView mTitle = (TextView) view.findViewById(R.id.tv_coupon_title);
		RelativeLayout mBTView = (RelativeLayout) view.findViewById(R.id.rl_item_coupon_line);
		ImageView mArraw = (ImageView) view.findViewById(R.id.iv_arrows1);
		TextView mMore = (TextView)view.findViewById(R.id.tv_more_content);
		if (isHaveCoupon) {
			mArraw.setVisibility(View.VISIBLE);
			mMore.setText("更多");
			mMore.setTextColor(mContext.getResources().getColor(R.color.mycoupon_red));
		}else {
			mArraw.setVisibility(View.INVISIBLE);
			mMore.setText("暂无");
			mMore.setTextColor(mContext.getResources().getColor(R.color.mycoupon_gray));
		}
		
		switch (number) {
		case 1:
			mTitle.setText("在线支付可用");
			mBTView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mContext,ShowFragmentActivity.class);
					intent.putExtra("type", 1);
					//intent.putExtra("store_id", 0);
					intent.putExtra("store_id", store_id);
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					mContext.startActivity(intent);
					
					
				}
			});
			break;
		case 2:
			mTitle.setText("到店支付可用");
			view.setTag("title2");
			mBTView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mContext,ShowFragmentActivity.class);
					intent.putExtra("type", 2);
					intent.putExtra("store_id", store_id);
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					mContext.startActivity(intent);
				}
			});
			break;
		default:
			break;
		}
		
		return view;
	}
//----------------------------------------在线支付可用优惠券---------------------------------------------------
	/**
	 * 初始化 在线支付可用的优惠券
	 * @param position
	 * @param convertView
	 * @return
	 */
	public class ViewHolderOnLine{
		TextView tv_online_coupon_number;
		TextView tv_coupon_new_type;
		TextView tv_coupon_new_xilie;
		TextView tv_online_coupon_bt;
		RelativeLayout rl_item_online_pay;
	}
	
	private ViewHolderOnLine  holder;
	private int onLineLevel;
	private int offLineLevel;
	private View initOnLineCoupon(final int position, View convertView,final int mObjectPosition) {
		 Object tag = null;
		if (convertView!=null) {
			
			tag = convertView.getTag();
		}
		if (null!=tag && tag instanceof ViewHolderOnLine) {
			holder = (ViewHolderOnLine) tag; 
		}else {
			holder = new ViewHolderOnLine();
			
			convertView = getView(R.layout.item_online_coupon);
			holder.tv_online_coupon_number = (TextView) convertView.findViewById(R.id.tv_online_coupon_number);
			holder.tv_coupon_new_type = (TextView) convertView.findViewById(R.id.tv_coupon_new_type);
			holder.tv_coupon_new_xilie = (TextView) convertView.findViewById(R.id.tv_coupon_new_xilie);
			holder.tv_online_coupon_bt = (TextView) convertView.findViewById(R.id.tv_online_coupon_bt);
			holder.rl_item_online_pay = (RelativeLayout) convertView.findViewById(R.id.rl_item_online_pay);
			convertView.setTag(holder);
		}
		
		OnLineCouponBean info = mOnLineList.get(position);
		
		if (info.isReceive_flag()||mCheckedList.contains(mObjectPosition)) {
			holder.tv_online_coupon_bt.setText("已领取");
			holder.tv_online_coupon_bt.setTextColor(mContext.getResources().getColor(R.color.mycoupon_gray));
			holder.rl_item_online_pay.setBackgroundResource(R.drawable.item_coupon_online_bg);
		}else {
			holder.tv_online_coupon_bt.setText("立即领取");
			holder.tv_online_coupon_bt.setTextColor(mContext.getResources().getColor(R.color.coupon_red));
			holder.rl_item_online_pay.setBackgroundResource(R.drawable.item_coupon_online);
		}
		holder.tv_online_coupon_bt.getPaint().setFakeBoldText(true); 
		
		
		switch (info.getCoupon_type()) {
		case 4:
			holder.tv_coupon_new_type.setText("代金券");
			break;

		default:
			break;
		}
		if (!TextUtils.isEmpty(info.getScope_desc())) {
			holder.tv_coupon_new_xilie.setText(info.getScope_desc());
		}
		holder.tv_online_coupon_number.setText(info.getCoupon_num_1());
		holder.rl_item_online_pay.setTag(info);
		holder.rl_item_online_pay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 页面详情的跳转
				
				OnLineCouponBean info = (OnLineCouponBean) v.getTag();
				Intent intent = new Intent(mContext, CouponinfoWebView.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra(KidAction.COUPON_ID, info.getCoupon_id()+"");
				intent.putExtra(KidAction.STORE_ID, store_id+"");
				intent.putExtra(KidAction.URL_TYPE, 8);
				mContext.startActivity(intent);
			}
		});
		
		holder.tv_online_coupon_bt.setTag(position);
		
		holder.tv_online_coupon_bt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 领取的点击事件
				if (mSelectListener!=null) {
					int index = (Integer)v.getTag();
					OnLineCouponBean info = mOnLineList.get(index);
					if (!info.isReceive_flag()&&!mCheckedList.contains(index)) {
						mSelectListener.onCheckListener(holder, info, mObjectPosition);
					}else {
						Toast.makeText(mContext, "已领取，无需再领", 0).show();
					}
				}
			}
		});
		
		
		
		
		return convertView;
	}
//----------------------------------------在线支付可用优惠券---------------------------------------------------
	public void setStore_id(String store_id) {
		this.store_id = store_id;
	}
	//----------------------------------------没有优惠券展示---------------------------------------------------	
	private View initNoCoupon(int number){
		View convertView = getView(R.layout.item_mycoupon_online_no_coupon);
		switch (number) {
		case 4:
			convertView.findViewById(R.id.tv_no_coupon).setBackgroundResource(R.drawable.no_coupon_bg);
		
			break;
		case 5:
			convertView.findViewById(R.id.tv_no_coupon).setBackgroundResource(R.drawable.no_store_coupon_bg);
			break;
		default:
			break;
		}
		
		return convertView;
	}
}
